<?php

namespace App\Http\Requests\Admin\Panx\Product\Asset;

use App\Http\Requests\ApiRequest;
use App\Models\Customer\Asset;
use App\Models\Panx\Category;
use Illuminate\Http\UploadedFile;

/**
 * @property string code
 * @property UploadedFile|null model_xml
 * @property string category_id
 * @property UploadedFile|null model_glass_android
 * @property UploadedFile|null model_glass_ios
 * @property string product_id
 */
class AssetUpdateRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $xml_type = is_file($this->model_xml) ? 'file' : 'string';
        $glass_android_type = is_file($this->model_glass_android) ? 'file' : 'string';
        $glass_ios_type = is_file($this->model_glass_ios) ? 'file' : 'string';

        return [
            'category_id'         => 'required|int|min:1|max:' . self::MAX_INT,
            'model_xml'           => 'nullable|'. $xml_type,
            'model_glass_android' => 'nullable|'. $glass_android_type,
            'model_glass_ios'     => 'nullable|'. $glass_ios_type,
            'code'                => 'required|string|max:' . self::MAX_STRING,
        ];
    }

    /**
     * @return UploadedFile|string|null
     * @throws \Exception
     */
    public function modelXml()
    {
        if ((int) $this->category_id === Category::GLASS_ID) {
            $model_xml = (new Asset)->uploadGlassAsset($this->model_glass_android, $this->model_glass_ios);
        } else {
            $model_xml = $this->model_xml;
        }

        return $model_xml;
    }
    /**
     * @return array
     */
//    public function messages()
//    {
//        return [
//            'product_id.*' => '商品的id必须是数值',
//            'model_xml.*'  => '素材文件必须是上传的文件',
//            'code.*'       => '素材的编码必须是字符串'
//        ];
//    }
}
